package common.com.androidbase.datarequest.view;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.sohu.sdk.common.toolbox.StringUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import common.com.androidbase.R;

/**
 * Created by sangchengjiang on 16/9/6.
 */
public class MapView extends LinearLayout {

    private static final String DEFAULT_TITLE = "Key:Value";

    private Context mContext;

    private LinearLayout titleContainer;
    private LinearLayout mapContainer;


    private TextView titleView;
    private ImageView arrow;

    public MapView(Context context) {
        super(context);
        init(context);
    }

    public MapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        setOrientation(VERTICAL);
        inflate(context, R.layout.vw_map, this);

        titleContainer = (LinearLayout) findViewById(R.id.layout_title_container);
        titleView = (TextView) findViewById(R.id.tv_map_title);
        titleView.setText(DEFAULT_TITLE);
        arrow = (ImageView) findViewById(R.id.iv_arrow);
        titleContainer.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mapContainer.getVisibility() == VISIBLE) {
                    mapContainer.setVisibility(GONE);
                    arrow.setImageResource(R.drawable.details_icon_arrowsdown);
                } else {
                    mapContainer.setVisibility(VISIBLE);
                    arrow.setImageResource(R.drawable.details_icon_arrowsup);
                }
            }
        });

        mapContainer = (LinearLayout) findViewById(R.id.layout_map_container);

    }

    public void setTitle(String text) {
        if (StringUtils.isBlank(text)) {
            return;
        }
        titleView.setText(text);
    }

    public void setMap(Map<String, String> map) {
        if (map == null) {
            return;
        }
        mapContainer.removeAllViews();
        Set<String> keySet = map.keySet();
        Iterator<String> keyIt = keySet.iterator();

        while(keyIt.hasNext()) {
            String key = keyIt.next();
            String value = map.get(key);
            TextView textView = new TextView(mContext);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(layoutParams);
            textView.setText(key + ": " + value);
            mapContainer.addView(textView);
        }

    }
}
